<template>
  <div class="login_box">
    <!-- 图片定位 -->
    <div class="img_box">
      <img src="../assets/img/psb.webp" alt="" />
    </div>
    <!-- form表单 -->
    <el-form
      ref="longinFormRef"
      label-width="0px"
      class="login_form"
      :model="loginForm"
      :rules="loginFormrules"
    >
      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input
          placeholder="请输入用户名!"
          v-model="loginForm.username"
          prefix-icon="iconfont icon-yonghuming"
        ></el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <el-input
          placeholder="请输入登录密码!"
          type="password"
          v-model="loginForm.password"
          prefix-icon="iconfont icon-mima"
        ></el-input>
      </el-form-item>
      <!-- 按钮 -->
      <el-form-item class="btn">
        <el-button class="btn1" type="primary" @click="login">登录</el-button>
        <el-button class="btn1" type="info" @click="resetLoginForm"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登录表单绑定的数据
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormrules: {
        username: [
          { required: true, message: "请输入用户名!", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 12 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码!", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    }
  },
  methods: {
    resetLoginForm() {
      // console.log(this);
      this.$refs.longinFormRef.resetFields()
    },
    login() {
      this.$refs.longinFormRef.validate(async (valid) => {
        // console.log(valid);   //valid为布尔值
        if (!valid) return
        const { data: res } = await this.$http.post("login", this.loginForm)
        // console.log(res);
        if (res.meta.status !== 200) {
          this.$message.error("登录失败！")
        } else {
          this.$message.success("登录成功！")
        }
        // console.log(res)
        window.sessionStorage.setItem("token", res.data.token)
        this.$router.push("/home")
      })
    },
  },
}
</script>

<style lang="less" >
.login_box {
  background-color: #fff;
  width: 450px;
  height: 300px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  .img_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    overflow: hidden;
    padding: 10px;
    box-shadow: 0 0 10px #ccc;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 25px 40px;
    .btn {
      display: flex;
      justify-content: flex-end;
      .btn1 {
        margin-left: 30px;
      }
    }
  }
}
</style>